package com.gsdkjgpt4.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gsdkjgpt4.sys.entity.User;
import com.gsdkjgpt4.sys.mapper.UserMapper;
import com.gsdkjgpt4.sys.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rendong
 * @since 2024-01-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, Object> login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getUserName, user.getUserName());
        wrapper.eq(User::getPassWord, user.getPassWord());
        User loginUser = this.getOne(wrapper);
        if (loginUser != null) {
            //token用UUID存储，存入redis
            String key = "user:" + UUID.randomUUID();
            loginUser.setPassWord(null);
            redisTemplate.opsForValue().set(key, loginUser, 30, TimeUnit.MINUTES);

            //返回数据
            Map<String, Object> data = new HashMap<>();
            data.put("token", key);
            return data;
        }
        return null;
    }

    public Map<String, Object> getUserInfo(String token) {
        // 从redis查询token
        Object obj = redisTemplate.opsForValue().get(token);
        User user = JSON.parseObject(JSON.toJSONString(obj), User.class);

        if (user != null) {
            Map<String, Object> data = new HashMap<>();
            List<User> userData = this.baseMapper.getUserByUserName(user.getUserName());
            data.put("userData", userData);

            return data;
        }
        return null;
    }

    public Map<String, Object> selectByUserId(int userId) {
        if (userId != 0) {
            Map<String, Object> data = new HashMap<>();
            List<User> userData = this.baseMapper.getUserByUserId(userId);
            data.put("userData", userData);

            return data;
        }
        return null;
    }

    public Map<String, Object> updateUserName(Integer userId, String userName, String passWord) {
        if(userId != 0 && userName != null && passWord != null){
            Map<String, Object> data = new HashMap<>();
            this.baseMapper.updateUserName(userId,userName,passWord);
            data.put("result", true);
            return data;
        }
        return null;
    }

    @Override
    public Map<String, Object> updatePosition(Integer userId) {
        if(userId != 0){
            Map<String, Object> data = new HashMap<>();
            this.baseMapper.updatePosition(userId);
            data.put("result",true);

            return data;
        }
        return null;
    }

    @Override
    public Map<String, Object> limitedNumber(Integer userId, Integer limitedNumber) {
        if(userId != 0){
            Map<String, Object> data = new HashMap<>();
            this.baseMapper.updateLimitedNumber(userId,limitedNumber);
            data.put("result",true);

            return data;
        }
        return null;
    }

    @Override
    public Map<String, Object> signUp(String userName, String passWord) {
        if(userName != null && passWord != null){
            Map<String, Object> data = new HashMap<>();
            //判断用户名是否重复
            List<User> userData = this.baseMapper.getUserByUserName(userName);
            if(userData.stream().findFirst().orElse(null) != null){
                data.put("result","repeat");
                return data;
            }
            //用户名不重复，开始注册
            this.baseMapper.signUp(userName,passWord);
            data.put("result", true);
            return data;
        }
        return null;
    }
}
